package first.endtoend

import org.springframework.dao.DataIntegrityViolationException

class AdminController
{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    def scaffold = true


    def index()
    {
        redirect(action: "list", params: params)
    }

    def list(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        [adminInstanceList: Admin.list(params), adminInstanceTotal: Admin.count()]
    }

    def create()
    {
        [adminInstance: new Admin(params)]

    }

    def save()
    {
        def adminInstance = new Admin(params)
        if (!adminInstance.save(flush: true))
        {
            render(view: "create", model: [adminInstance: adminInstance])
            return
        }

        println("***" + params)
        Locality localityInstance = Locality.findById(params.locality.id)
        //verify if this locality is already managed by this admin

        LocalityManager localityManagersByAdmin = LocalityManager.findByLocalityAndManager(localityInstance, adminInstance)
        LocalityManager localityManager
        if (!localityManagersByAdmin)
        {
            localityManager = new LocalityManager(manager: adminInstance, locality: localityInstance).save(flush: true)
            adminInstance.localitieManagers = new ArrayList<LocalityManager>()
            adminInstance.localitieManagers.add(localityManager)

        } else
        {

        }


        redirect(action: "show", id: adminInstance.id)
    }

    def show(Long id)
    {
        def adminInstance = Admin.get(id)
        if (!adminInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), id])
            redirect(action: "list")
            return
        }

        [adminInstance: adminInstance]
    }

    def edit(Long id)
    {
        def adminInstance = Admin.get(id)
        if (!adminInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), id])
            redirect(action: "list")
            return
        }

        [adminInstance: adminInstance]

    }

    def update(Long id, Long version)
    {
        def adminInstance = Admin.get(id)
        if (!adminInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), id])
            redirect(action: "list")
            return
        }

        if (version != null)
        {
            if (adminInstance.version > version)
            {
                adminInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                        [message(code: 'admin.label', default: 'Admin')] as Object[],
                        "Another user has updated this Admin while you were editing")
                render(view: "edit", model: [adminInstance: adminInstance])
                return
            }
        }
        Locality localityInstance = Locality.findById(params.locality.id)

        LocalityManager localityManagersByAdmin = LocalityManager.findByLocalityAndManager(localityInstance, adminInstance)
        LocalityManager localityManager
        if (!localityManagersByAdmin)
        {
            localityManager = new LocalityManager(manager: adminInstance, locality: localityInstance).save(flush: true)
            adminInstance.localitieManagers = new ArrayList<LocalityManager>()
            adminInstance.localitieManagers.add(localityManager)
        } else
        {

        }
/*
        redirect(action: "create", controller: "localityManager", params: ['admin.id':adminInstance.id, 'level.id':adminInstance.level.id])
*/

        adminInstance.properties = params

        if (!adminInstance.save(flush: true))
        {
            render(view: "edit", model: [adminInstance: adminInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'admin.label', default: 'Admin'), adminInstance.id])
        redirect(action: "show", id: adminInstance.id)

    }

    def delete(Long id)
    {
        def adminInstance = Admin.get(id)
        if (!adminInstance)
        {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), id])
            redirect(action: "list")
            return
        }

        try
        {
            adminInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'admin.label', default: 'Admin'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e)
        {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'admin.label', default: 'Admin'), id])
            redirect(action: "show", id: id)
        }
    }

}
